package xw.qxypt.modules.socket.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import xw.qxypt.modules.baseinfo.entity.LhpDataRoadDetectorEntity;
import xw.qxypt.modules.baseinfo.entity.LhpDataVisibilityEntity;
import xw.qxypt.modules.baseinfo.entity.LhpDataWeatherStationEntity;
import xw.qxypt.modules.baseinfo.entity.LhpEquipmentEntity;
import xw.qxypt.modules.baseinfo.service.LhpDataRoadDetectorService;
import xw.qxypt.modules.baseinfo.service.LhpDataWeatherStationService;
import xw.qxypt.modules.baseinfo.service.LhpInstallationPointService;
import xw.qxypt.modules.socket.entity.DataCollection;
import xw.qxypt.modules.socket.util.HexString2BinaryStr;
import xw.qxypt.modules.socket.util.HspUtil;
import xw.qxypt.modules.socket.util.SocketUtil;

import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service("collectorSocketServer")
public class CollectorSocketServer {

    private static final int PORT = 23101;// 监听的端口号


    @Autowired
    private LhpInstallationPointService lhpInstallationPointService;
    @Autowired
    private LhpDataWeatherStationService lhpDataWeatherStationService;
    @Autowired
    private LhpDataRoadDetectorService lhpDataRoadDetectorService;

    public void init() {
        //初始化设备连接列表
        lhpInstallationPointService.refreshDataCollectionMap(HspUtil.map);
        try {
            System.out.println("开启socket端口"+PORT);
            ServerSocket serverSocket = new ServerSocket(PORT);
            while (true) {
                // 一旦有堵塞, 则表示服务器与客户端获得了连接
                Socket client = serverSocket.accept();
                //client.setTcpNoDelay(true);
                // 处理这次连接
                new HandlerThread(client);
            }
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("服务器异常: " + e.getMessage());
        }
    }


    public void delay(){
        try {
            Thread.sleep(20000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    //发送报文
    public  void poll(){
        for (String sensorNo:HspUtil.map.keySet()) {
            if(sensorNo.startsWith("AB")){
                DataCollection dc = HspUtil.map.get(sensorNo);
                Socket socket = dc.getSocket();
                if(socket!=null && !socket.isClosed()){
                    System.out.println("------------------------pull----");
                    new pollThread(dc);
                }
            }
        }
    }

    /**
     * 发送二进制报文
     * @param mbapADUMeter
     */
    public String sendData(String mbapADUMeter, OutputStream out) {
        // System.out.println("5.向采集器：" + lgpCollector.getIp()+ "，发送的指令是：" +mes);
        String mes = null;
        byte[] buffer = new byte[mbapADUMeter.length() / 2];
        String contentHex = mbapADUMeter;
        for (int i = 0; i < contentHex.length(); i += 2) {
            try {
                buffer[i / 2] = (byte) (0xff & Integer.parseInt(contentHex
                        .substring(i, i + 2), 16));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        try {
            out.write(buffer);
            out.flush();
        } catch (IOException e) {
            mes = "sendFalse";
        }
        return mes;
    }
    /***************************数据报文接收开始******************************/
    private class HandlerThread implements Runnable {

        private String sensorNo;
        private Socket socket;
        private Date collectDate = new Date();
        private final int BUFSIZE = 1024 * 8;// 初始接收数据的总字节数
        byte[] receiveBuf = new byte[BUFSIZE];// 接收数据的缓冲区

        public HandlerThread(Socket client) {
            socket = client;
            new Thread(this).start();
        }

        public void run() {

            try {
                // 装饰流BufferedReader封装输入流（接收客户端的流）
                BufferedInputStream bis = new BufferedInputStream(
                        socket.getInputStream());

                DataInputStream dis = new DataInputStream(bis);
                byte[] bytes = new byte[1024]; // 一次读取一个byte
                String ret = "";

                int recvMsgSize;

                while ((recvMsgSize = dis.read(bytes)) != -1) {
                    ret = SocketUtil.toHexString(bytes);
                    ret = ret.substring(0, recvMsgSize * 2);
                    if (dis.available() == 0) { //一个请求
                        System.out.println("ret*******"+ret);
                        //注册包
                        if(ret.startsWith("AA")||ret.startsWith("AB")){
                            String no = ret;
                            //查找是否存在
                            if(HspUtil.map.containsKey(no)){
                                Socket clientSocket = HspUtil.map.get(no).getSocket();
                                if(clientSocket != null){
                                    try{
                                        if(!clientSocket.isClosed()){
                                            clientSocket.close();
                                            System.out.println("关闭原有"+ret+"链接！");
                                        }
                                    }catch(Exception e){

                                    }
                                }
                                //存放会话
                                HspUtil.map.get(no).setSocket(socket);
                                sensorNo = no;
//            						System.out.println(ret);
                            }
                            System.out.println(ret+"建立连接！");
                        }
                        //如果是dtu设置返回数据
                        if(ret.startsWith("7573722E636E23") || ret.startsWith("7777772e7573722e636e23")){
                            String ret1 = SocketUtil.hexStringToString(ret);
                            HspUtil.dtuMap.put(sensorNo,ret1.trim());
                            System.out.println(ret1.trim());
                        }

                        //液位
                        if(ret.startsWith("000000000005000302")) {
                            String A1 = ret.substring(18,22);
                            double x = Integer.parseInt(A1, 16)*2.0/1000;
                            String A = String.valueOf(x)+"米";
                            if(HspUtil.map.containsKey(sensorNo)) {
                                HashMap<String,String> valueMap = null;
                                if(HspUtil.eptMap1.containsKey(sensorNo)) {
                                    valueMap = HspUtil.eptMap1.get(sensorNo);
                                }else{
                                    valueMap = new HashMap<String,String>();
                                    HspUtil.eptMap1.put(sensorNo,valueMap);
                                }
                                valueMap.put("yewei",A);
                            }
                        }
                        //读取Y值
                        if(ret.startsWith("000000000007000204")) {
                            String A1 = ret.substring(22,24);
                            String yStatus = HexString2BinaryStr.hexString2BinaryStr(A1);
                            String ddfK = String.valueOf(yStatus.charAt(1));
                            String A2 = ret.substring(24,26);
                            String yStatus1 = HexString2BinaryStr.hexString2BinaryStr(A2);
                            String bpqStatus = String.valueOf(yStatus1.charAt(6));
                            String ddfG = String.valueOf(yStatus1.charAt(5));
                            if(HspUtil.map.containsKey(sensorNo)) {
                                HashMap<String,String> valueMap = null;
                                if(HspUtil.eptMap1.containsKey(sensorNo)) {
                                    valueMap = HspUtil.eptMap1.get(sensorNo);
                                }else{
                                    valueMap = new HashMap<String,String>();
                                    HspUtil.eptMap1.put(sensorNo,valueMap);
                                }
                                valueMap.put("ddfK",ddfK);
                                valueMap.put("bpqStatus",bpqStatus);
                                valueMap.put("ddfG",ddfG);
                            }
                        }
                        //读取X值
                        if(ret.startsWith("000000000005000202")) {
                            String A1 = ret.substring(18,20);
                            String xStatus = HexString2BinaryStr.hexString2BinaryStr(A1);
                            String systemStatus = String.valueOf(xStatus.charAt(7));
                            String ddfKdw = String.valueOf(xStatus.charAt(2));
                            String ddfGdw= String.valueOf(xStatus.charAt(1));
                            if(HspUtil.map.containsKey(sensorNo)) {
                                HashMap<String,String> valueMap = null;
                                if(HspUtil.eptMap1.containsKey(sensorNo)) {
                                    valueMap = HspUtil.eptMap1.get(sensorNo);
                                }else{
                                    valueMap = new HashMap<String,String>();
                                    HspUtil.eptMap1.put(sensorNo,valueMap);
                                }
                                valueMap.put("systemStatus",systemStatus);
                                valueMap.put("ddfKdw",ddfKdw);
                                valueMap.put("ddfGdw",ddfGdw);
                            }
                        }
                        //读取液压和喷头号
                        if(ret.startsWith("000000000015000312")) {
                            String yeya = ret.substring(18,26);
                            String	ptNum = String.valueOf(Integer.parseInt(ret.substring(50,54),16));
                            if(HspUtil.map.containsKey(sensorNo)) {
                                HashMap<String,String> valueMap = null;
                                if(HspUtil.eptMap1.containsKey(sensorNo)) {
                                    valueMap = HspUtil.eptMap1.get(sensorNo);
                                }else{
                                    valueMap = new HashMap<String,String>();
                                    HspUtil.eptMap1.put(sensorNo,valueMap);
                                }
                                //valueMap.put("yeya",yeya);
                                valueMap.put("ptNum",ptNum);

                            }
                        }
                        //凝冰设备状态

                        if(ret.startsWith("000000000007000304")) {
                            String A = ret.substring(18,22);
                            if(HspUtil.map.containsKey(sensorNo)) {
                                HashMap<String,String> valueMap = null;
                                if(HspUtil.eptMap1.containsKey(sensorNo)) {
                                    valueMap = HspUtil.eptMap1.get(sensorNo);
                                }else{
                                    valueMap = new HashMap<String,String>();
                                    HspUtil.eptMap1.put(sensorNo,valueMap);
                                }
                                valueMap.put("status",A);
                            }
                        }
                    }
                }
                socket.shutdownInput();//关闭输入流

            } catch (IOException e) {
                System.out.println("会话中断！");
                try {
                    socket.close();
                } catch (IOException e1) {

                    e1.printStackTrace();
                }
            }finally {


            }
        }
    }
    /***************************数据报文接收结束******************************/


    /****************************发送报文开始********************************/
    private class pollThread implements Runnable {

        private DataCollection dataCollection;

        public pollThread(DataCollection dc){

            dataCollection = dc;
            Socket socket = dataCollection.getSocket();
            if(socket!=null && !socket.isClosed()){
                new Thread(this).start();
            }
        }
        public void run() {

            Socket socket = dataCollection.getSocket();

            if(socket!=null && !socket.isClosed()){
                HashMap<String, LhpEquipmentEntity> hmap=dataCollection.getLemMap();
                for (Map.Entry<String,LhpEquipmentEntity> entryLem :hmap .entrySet()){
                    try{
                        LhpEquipmentEntity lem = entryLem.getValue();
                        //是凝冰的设备
                        if(lem.getManufactorType().equals("01") && lem.getEquipmentType().equals("04")) {
                            //获取当前凝冰设备安装点
                            String point = entryLem.getKey();
                            //通过凝冰的安装点信息 去查询本安装点的设备信息
                            if("AB025A0201".equals(point)) {
                                //通过凝冰的pointcode去查询本安装点的设备信息
                                String point1 = "AA025A0102";
                                String point2 = "AA025A0201";
                                //获取安装点的设备列表
                                Map<String,LhpEquipmentEntity> leMap1 = HspUtil.map.get(point1).getLemMap();
                                Map<String,LhpEquipmentEntity> leMap = HspUtil.map.get(point2).getLemMap();
                                String roadTemp1 = "00.00";
                                String roadTemp2 = "00.00";
                                String airTemp = "00.00";
                                String wendSpeed = "00.00";
                                String friction = "00.00";
                                String salineConcentration = "00.00";
                                String waterFilmHeight = "00.00";
                                //循环此安装点的设备，根据不同类型查询相关的参数
                                for(Map.Entry<String,LhpEquipmentEntity> en :leMap.entrySet()) {
                                    LhpEquipmentEntity le = en.getValue();
                                    if("01".equals(le.getEquipmentType())) {
                                        List<LhpDataWeatherStationEntity> entityList = lhpDataWeatherStationService.list(
                                                new QueryWrapper<LhpDataWeatherStationEntity>()
                                                        .eq("equipment_id",le.getId())
                                                        .orderByDesc("data_time")
                                                        .last("limit 1")
                                        );
                                        if(entityList.size()>0){
                                            //airTemp = entityList.get(0).getAmbientTemp();
                                            wendSpeed = entityList.get(0).getWindSpeed();
                                        }
                                    }
                                    if("03".equals(le.getEquipmentType())||"05".equals(le.getEquipmentType())||"06".equals(le.getEquipmentType())) {
                                        List<LhpDataRoadDetectorEntity> entityList = lhpDataRoadDetectorService.list(
                                                new QueryWrapper<LhpDataRoadDetectorEntity>()
                                                        .eq("equipment_id",le.getId())
                                                        .orderByDesc("data_time")
                                                        .last("limit 1")
                                        );
                                        if(entityList.size()>0){
                                            roadTemp2 = entityList.get(0).getRoadSurfaceTemp();
                                            friction = entityList.get(0).getFriction();
                                            salineConcentration = entityList.get(0).getSalineConcentrationNacl();
                                            if(salineConcentration == null ||"".equals(salineConcentration)) {
                                                salineConcentration = "00.00";
                                            }
                                            waterFilmHeight = entityList.get(0).getWaterFilmHeight();
                                        }
                                    }
                                }
                                for(Map.Entry<String,LhpEquipmentEntity> en :leMap1.entrySet()) {
                                    LhpEquipmentEntity le = en.getValue();
                                    if("01".equals(le.getEquipmentType())) {
                                        List<LhpDataWeatherStationEntity> entityList = lhpDataWeatherStationService.list(
                                                new QueryWrapper<LhpDataWeatherStationEntity>()
                                                        .eq("equipment_id",le.getId())
                                                        .orderByDesc("data_time")
                                                        .last("limit 1")
                                        );
                                        if(entityList.size()>0){
                                           // airTemp = entityList.get(0).getAmbientTemp();
                                            wendSpeed = entityList.get(0).getWindSpeed();
                                        }
                                    }
                                    if("03".equals(le.getEquipmentType())||"05".equals(le.getEquipmentType())||"06".equals(le.getEquipmentType())) {
                                        List<LhpDataRoadDetectorEntity> entityList = lhpDataRoadDetectorService.list(
                                                new QueryWrapper<LhpDataRoadDetectorEntity>()
                                                        .eq("equipment_id",le.getId())
                                                        .orderByDesc("data_time")
                                                        .last("limit 1")
                                        );
                                        if(entityList.size()>0){
                                            roadTemp1 = entityList.get(0).getRoadSurfaceTemp();
                                        }
                                    }
                                }

                                //拼发送指令
                                if(roadTemp1 != null) {
                                    String sendMessage1 = "00000000000600061096";
                                    if("00.00".equals(roadTemp1)) {
                                        sendMessage1 += "0000";
                                    }else {
                                        int a = Math.round(Float.parseFloat(roadTemp1)*10);
                                        if(a>=0) {
                                            String b = Integer.toHexString(a);
                                            String c = b;
                                            for(int i=0;i<4-b.length();i++) {
                                                c = "0"+c;
                                            }
                                            sendMessage1 += c;
                                        }else {
                                            String b = Integer.toHexString(a);
                                            String c = b.substring(b.length()-4);
                                            sendMessage1 += c;
                                        }
                                    }
                                    sendData(sendMessage1, socket.getOutputStream());
                                    try {
                                        Thread.sleep(1000);
                                    } catch (InterruptedException e) {
                                        e.printStackTrace();
                                    }
                                }
                                if(roadTemp2 != null) {
                                    String sendMessage1 = "00000000000600061097";
                                    if("00.00".equals(roadTemp2)) {
                                        sendMessage1 += "0000";
                                    }else {
                                        int a = Math.round(Float.parseFloat(roadTemp2)*10);
                                        if(a>=0) {
                                            String b = Integer.toHexString(a);
                                            String c = b;
                                            for(int i=0;i<4-b.length();i++) {
                                                c = "0"+c;
                                            }
                                            sendMessage1 += c;
                                        }else {
                                            String b = Integer.toHexString(a);
                                            String c = b.substring(b.length()-4);
                                            sendMessage1 += c;
                                        }
                                    }
                                    sendData(sendMessage1, socket.getOutputStream());
                                    try {
                                        Thread.sleep(1000);
                                    } catch (InterruptedException e) {
                                        e.printStackTrace();
                                    }
                                }
                                if(airTemp != null) {
                                    String sendMessage1 = "00000000000600061099";
                                    if("00.00".equals(airTemp)) {
                                        sendMessage1 += "0000";
                                    }else {
                                        int a = Math.round(Float.parseFloat(airTemp)*10);
                                        if(a>=0) {
                                            String b = Integer.toHexString(a);
                                            String c = b;
                                            for(int i=0;i<4-b.length();i++) {
                                                c = "0"+c;
                                            }
                                            sendMessage1 += c;
                                        }else {
                                            String b = Integer.toHexString(a);
                                            String c = b.substring(b.length()-4);
                                            sendMessage1 += c;
                                        }
                                    }
                                    sendData(sendMessage1, socket.getOutputStream());
                                    try {
                                        Thread.sleep(1000);
                                    } catch (InterruptedException e) {
                                        e.printStackTrace();
                                    }
                                }
                                if(wendSpeed != null) {
                                    String sendMessage1 = "0000000000060006109A";
                                    if("00.00".equals(wendSpeed)) {
                                        sendMessage1 += "0000";
                                    }else {
                                        int a = Math.round(Float.parseFloat(wendSpeed)*36);
                                        if(a>=0) {
                                            String b = Integer.toHexString(a);
                                            String c = b;
                                            for(int i=0;i<4-b.length();i++) {
                                                c = "0"+c;
                                            }
                                            sendMessage1 += c;
                                        }else {
                                            String b = Integer.toHexString(a);
                                            String c = b.substring(b.length()-4);
                                            sendMessage1 += c;
                                        }
                                    }
                                    sendData(sendMessage1, socket.getOutputStream());
                                    try {
                                        Thread.sleep(1000);
                                    } catch (InterruptedException e) {
                                        e.printStackTrace();
                                    }
                                }
                                if(friction != null) {
                                    String sendMessage1 = "00000000000600061098";
                                    if("00.00".equals(friction)) {
                                        sendMessage1 += "0000";
                                    }else {
                                        int a = Math.round(Float.parseFloat(friction)*10);
                                        if(a>=0) {
                                            String b = Integer.toHexString(a);
                                            String c = b;
                                            for(int i=0;i<4-b.length();i++) {
                                                c = "0"+c;
                                            }
                                            sendMessage1 += c;
                                        }else {
                                            String b = Integer.toHexString(a);
                                            String c = b.substring(b.length()-4);
                                            sendMessage1 += c;
                                        }
                                    }
                                    sendData(sendMessage1, socket.getOutputStream());
                                    try {
                                        Thread.sleep(1000);
                                    } catch (InterruptedException e) {
                                        e.printStackTrace();
                                    }
                                }
                                if(salineConcentration != null) {
                                    String sendMessage1 = "0000000000060006109B";
                                    if("00.00".equals(salineConcentration)) {
                                        sendMessage1 += "0000";
                                    }else {
                                        int a = Math.round(Float.parseFloat(salineConcentration)*10);
                                        if(a>=0) {
                                            String b = Integer.toHexString(a);
                                            String c = b;
                                            for(int i=0;i<4-b.length();i++) {
                                                c = "0"+c;
                                            }
                                            sendMessage1 += c;
                                        }else {
                                            String b = Integer.toHexString(a);
                                            String c = b.substring(b.length()-4);
                                            sendMessage1 += c;
                                        }
                                    }
                                    sendData(sendMessage1, socket.getOutputStream());
                                    try {
                                        Thread.sleep(1000);
                                    } catch (InterruptedException e) {
                                        e.printStackTrace();
                                    }
                                }
                                if(waterFilmHeight != null) {
                                    String sendMessage1 = "0000000000060006109C";
                                    if("00.00".equals(waterFilmHeight)) {
                                        sendMessage1 += "0000";
                                    }else {
                                        int a = Math.round(Float.parseFloat(waterFilmHeight)*10);
                                        if(a>=0) {
                                            String b = Integer.toHexString(a);
                                            String c = b;
                                            for(int i=0;i<4-b.length();i++) {
                                                c = "0"+c;
                                            }
                                            sendMessage1 += c;
                                        }else {
                                            String b = Integer.toHexString(a);
                                            String c = b.substring(b.length()-4);
                                            sendMessage1 += c;
                                        }
                                    }
                                    sendData(sendMessage1, socket.getOutputStream());
                                    try {
                                        Thread.sleep(1000);
                                    } catch (InterruptedException e) {
                                        e.printStackTrace();
                                    }
                                }
                            }else {
                                //通过凝冰的pointcode去查询本安装点的设备信息
                                String point1 = null;
                                point1 = "AA"+point.substring(2);
                                //point1 = "AA2502030911";
                                //获取安装点的设备列表
                                Map<String,LhpEquipmentEntity> leMap = HspUtil.map.get(point1).getLemMap();
                                String roadTemp = "00.00";
                                String airTemp = "00.00";
                                String wendSpeed = "00.00";
                                String friction = "00.00";
                                String salineConcentration = "00.00";
                                String waterFilmHeight = "00.00";
                                //循环此安装点的设备，根据不同类型查询相关的参数
                                for(Map.Entry<String,LhpEquipmentEntity> en :leMap.entrySet()) {
                                    LhpEquipmentEntity le = en.getValue();
                                    if("01".equals(le.getEquipmentType())) {
                                        List<LhpDataWeatherStationEntity> entityList = lhpDataWeatherStationService.list(
                                                new QueryWrapper<LhpDataWeatherStationEntity>()
                                                        .eq("equipment_id",le.getId())
                                                        .orderByDesc("data_time")
                                                        .last("limit 1")
                                        );
                                        if(entityList.size()>0){
                                            //airTemp = entityList.get(0).getAmbientTemp();
                                            wendSpeed = entityList.get(0).getWindSpeed();
                                        }
                                    }
                                    if("03".equals(le.getEquipmentType())||"05".equals(le.getEquipmentType())||"06".equals(le.getEquipmentType())) {
                                        List<LhpDataRoadDetectorEntity> entityList = lhpDataRoadDetectorService.list(
                                                new QueryWrapper<LhpDataRoadDetectorEntity>()
                                                        .eq("equipment_id",le.getId())
                                                        .orderByDesc("data_time")
                                                        .last("limit 1")
                                        );
                                        if(entityList.size()>0){
                                            roadTemp = entityList.get(0).getRoadSurfaceTemp();
                                            friction = entityList.get(0).getFriction();
                                            salineConcentration = entityList.get(0).getSalineConcentrationNacl();
                                            if(salineConcentration == null ||"".equals(salineConcentration)) {
                                                salineConcentration = "00.00";
                                            }
                                            waterFilmHeight = entityList.get(0).getWaterFilmHeight();
                                        }
                                    }
                                }

                                //拼发送指令
                                if(roadTemp != null) {
                                    String sendMessage1 = "00000000000600061097";
                                    if("00.00".equals(roadTemp)) {
                                        sendMessage1 += "0000";
                                    }else {
                                        int a = Math.round(Float.parseFloat(roadTemp)*10);
                                        if(a>=0) {
                                            String b = Integer.toHexString(a);
                                            String c = b;
                                            for(int i=0;i<4-b.length();i++) {
                                                c = "0"+c;
                                            }
                                            sendMessage1 += c;
                                        }else {
                                            String b = Integer.toHexString(a);
                                            String c = b.substring(b.length()-4);
                                            sendMessage1 += c;
                                        }
                                    }
                                    sendData(sendMessage1, socket.getOutputStream());
                                    try {
                                        Thread.sleep(1000);
                                    } catch (InterruptedException e) {
                                        e.printStackTrace();
                                    }
                                }
                                if(airTemp != null) {
                                    String sendMessage1 = "00000000000600061099";
                                    if("00.00".equals(airTemp)) {
                                        sendMessage1 += "0000";
                                    }else {
                                        int a = Math.round(Float.parseFloat(airTemp)*10);
                                        if(a>=0) {
                                            String b = Integer.toHexString(a);
                                            String c = b;
                                            for(int i=0;i<4-b.length();i++) {
                                                c = "0"+c;
                                            }
                                            sendMessage1 += c;
                                        }else {
                                            String b = Integer.toHexString(a);
                                            String c = b.substring(b.length()-4);
                                            sendMessage1 += c;
                                        }
                                    }
                                    sendData(sendMessage1, socket.getOutputStream());
                                    try {
                                        Thread.sleep(1000);
                                    } catch (InterruptedException e) {
                                        e.printStackTrace();
                                    }
                                }
                                if(wendSpeed != null) {
                                    String sendMessage1 = "0000000000060006109A";
                                    if("00.00".equals(wendSpeed)) {
                                        sendMessage1 += "0000";
                                    }else {
                                        int a = Math.round(Float.parseFloat(wendSpeed)*10);
                                        if(a>=0) {
                                            String b = Integer.toHexString(a);
                                            String c = b;
                                            for(int i=0;i<4-b.length();i++) {
                                                c = "0"+c;
                                            }
                                            sendMessage1 += c;
                                        }else {
                                            String b = Integer.toHexString(a);
                                            String c = b.substring(b.length()-4);
                                            sendMessage1 += c;
                                        }
                                    }
                                    sendData(sendMessage1, socket.getOutputStream());
                                    try {
                                        Thread.sleep(1000);
                                    } catch (InterruptedException e) {
                                        e.printStackTrace();
                                    }
                                }
                                if(friction != null) {
                                    String sendMessage1 = "00000000000600061098";
                                    if("00.00".equals(friction)) {
                                        sendMessage1 += "0000";
                                    }else {
                                        int a = Math.round(Float.parseFloat(friction)*10);
                                        if(a>=0) {
                                            String b = Integer.toHexString(a);
                                            String c = b;
                                            for(int i=0;i<4-b.length();i++) {
                                                c = "0"+c;
                                            }
                                            sendMessage1 += c;
                                        }else {
                                            String b = Integer.toHexString(a);
                                            String c = b.substring(b.length()-4);
                                            sendMessage1 += c;
                                        }
                                    }
                                    sendData(sendMessage1, socket.getOutputStream());
                                    try {
                                        Thread.sleep(1000);
                                    } catch (InterruptedException e) {
                                        e.printStackTrace();
                                    }
                                }
                                if(salineConcentration != null) {
                                    String sendMessage1 = "0000000000060006109B";
                                    if("00.00".equals(salineConcentration)) {
                                        sendMessage1 += "0000";
                                    }else {
                                        int a = Math.round(Float.parseFloat(salineConcentration)*10);
                                        if(a>=0) {
                                            String b = Integer.toHexString(a);
                                            String c = b;
                                            for(int i=0;i<4-b.length();i++) {
                                                c = "0"+c;
                                            }
                                            sendMessage1 += c;
                                        }else {
                                            String b = Integer.toHexString(a);
                                            String c = b.substring(b.length()-4);
                                            sendMessage1 += c;
                                        }
                                    }
                                    sendData(sendMessage1, socket.getOutputStream());
                                    try {
                                        Thread.sleep(1000);
                                    } catch (InterruptedException e) {
                                        e.printStackTrace();
                                    }
                                }
                                if(waterFilmHeight != null) {
                                    String sendMessage1 = "0000000000060006109C";
                                    if("00.00".equals(waterFilmHeight)) {
                                        sendMessage1 += "0000";
                                    }else {
                                        int a = Math.round(Float.parseFloat(waterFilmHeight)*10);
                                        if(a>=0) {
                                            String b = Integer.toHexString(a);
                                            String c = b;
                                            for(int i=0;i<4-b.length();i++) {
                                                c = "0"+c;
                                            }
                                            sendMessage1 += c;
                                        }else {
                                            String b = Integer.toHexString(a);
                                            String c = b.substring(b.length()-4);
                                            sendMessage1 += c;
                                        }
                                    }
                                    sendData(sendMessage1, socket.getOutputStream());
                                    try {
                                        Thread.sleep(1000);
                                    } catch (InterruptedException e) {
                                        e.printStackTrace();
                                    }
                                }
                            }
                            String sendMessage2 = "000000000006000310640001";
                            sendData(sendMessage2, socket.getOutputStream());
                            Thread.sleep(1000);
                            String sendMessage3 = "0000000000060003104A0002";
                            sendData(sendMessage3, socket.getOutputStream());
                            Thread.sleep(1000);
                            String sendMessage4 = "000000000006000205000020";
                            sendData(sendMessage4, socket.getOutputStream());
                            Thread.sleep(1000);
                            String sendMessage5 = "000000000006000204000010";
                            sendData(sendMessage5, socket.getOutputStream());
                            Thread.sleep(1000);
                            String sendMessage6 = "0000000000060003102C0009";
                            sendData(sendMessage6, socket.getOutputStream());
                        }

                        delay();
                    }catch(Exception e){
                        e.printStackTrace();
                    }
                }
            }
        }
    }
    /****************************发送报文结束********************************/

}
